package com.aaa.service.impl;

import com.aaa.dao.ActivityMapper;
import com.aaa.mq.ActivityMqConfig;
import com.aaa.service.ActivityService;
import com.aaa.utils.ActivityUtil;
import com.wsc.entity.Activity;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Resource
    ActivityMapper activityMapper;

    @Resource
    ActivityUtil activityUtil;

    @Override
    public int addActivity(Activity activity) {
        // 添加活动到数据库中
        int i = activityMapper.insert(activity);

        // 设置自动开始倒计时：rabbitmq
        if (i == 1) {
            activityUtil.sendMsg(ActivityMqConfig.ACTIVITY_EX, ActivityMqConfig.ACTIVITY_KEY_START, activity, activity.getStart());
        }
        return i;
    }

    @Override
    public int update(Activity newActivity) {
        int i;
        Activity oldActivity = activityMapper.selectById(newActivity.getAid());
        if(oldActivity.getStart().equals(newActivity.getStart())){
            i=activityMapper.updateById(newActivity);
        }else {
            newActivity.setVersion(oldActivity.getVersion()+1);
            i=activityMapper.updateById(newActivity);
            activityUtil.sendMsg(ActivityMqConfig.ACTIVITY_EX,
                    ActivityMqConfig.ACTIVITY_KEY_START,newActivity,newActivity.getStart());
        }
        return i;
    }

}
